package com.bos.controller.base.imp;

import com.bos.controller.base.BaseController;
import com.bos.utils.Date2Json;
import com.bos.utils.PageBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.hibernate.criterion.DetachedCriteria;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @author Administrator
 */
public class BaseControllerImp<T>  implements BaseController<T> {

    protected DetachedCriteria detachedCriteria = null;

    protected PageBean pageBean = null;

    public void setPageAndRows(int page,int rows){
        pageBean.setCurrentPage(page);
        pageBean.setPageSize(rows);
    }

    public BaseControllerImp() {
    }

    public BaseControllerImp(PageBean pageBean){
        this.pageBean=pageBean;
        ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
        Class<T> entityClass = (Class<T>) actualTypeArguments[0];
        detachedCriteria=DetachedCriteria.forClass(entityClass);
        pageBean.setDetachedCriteria(detachedCriteria);
    }

    public String java2json(String [] exclude){
        JsonConfig pageConfig=new JsonConfig();
        //排除多余的项,把必要的数据转成json返回
        pageConfig.setExcludes(exclude);
        String json = JSONObject.fromObject(pageBean,pageConfig).toString();
        return json;
    }

    public String java2json(Object object,String [] exclude){
        JsonConfig pageConfig=new JsonConfig();
        //排除多余的项,把必要的数据转成json返回
        pageConfig.setExcludes(exclude);
        String json = JSONObject.fromObject(object,pageConfig).toString();
        return json;
    }

    public String java2json(List list, String [] exclude){
        JsonConfig pageConfig=new JsonConfig();
        //排除多余的项,把必要的数据转成json返回
        pageConfig.setExcludes(exclude);
        String json = JSONArray.fromObject(list,pageConfig).toString();
        return json;
    }

    public String java2json(Set set, String [] exclude){
        JsonConfig pageConfig=new JsonConfig();
        //排除多余的项,把必要的数据转成json返回
        pageConfig.setExcludes(exclude);
        String json = JSONArray.fromObject(set,pageConfig).toString();
        return json;
    }

    /**
     * 转换包含日期的对象为json格式数据*/
    public String java2jsonAndDate(Set set, String [] exclude){
        JsonConfig jsonConfig=new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class,new Date2Json());
        //排除多余的项,把必要的数据转成json返回
        jsonConfig.setExcludes(exclude);
        String json = JSONArray.fromObject(set,jsonConfig).toString();
        return json;
    }

    public String java2jsonAndDate(Object o, String [] exclude){
        JsonConfig jsonConfig=new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class,new Date2Json());
        //排除多余的项,把必要的数据转成json返回
        jsonConfig.setExcludes(exclude);
        String json = JSONObject.fromObject(o,jsonConfig).toString();
        return json;
    }

    /**
     * 根据日期类型转换为json格式数据*/
    public String java2jsonAndDate(String [] exclude){
        JsonConfig jsonConfig=new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class,new Date2Json());
        //排除多余的项,把必要的数据转成json返回
        jsonConfig.setExcludes(exclude);
        String json = JSONObject.fromObject(pageBean,jsonConfig).toString();
        return json;
    }

}
